package com.mizhiquan.util;

import com.sun.org.apache.xpath.internal.operations.Bool;

import java.util.Random;

/**
 * Created by Shane on 2017/7/12.
 */
public class RandomCodeUtil {
    /**
     * 获取length长度的是否允许0开头的随机数字验证码
     * @param length
     * @param canZeroStart
     * @return
     */
    public static String getRandomNum(int length,boolean canZeroStart) {
        String num = "";
        for (int i = 0; i < length; i++) {
            Random rd = new Random();
            num = num + String.valueOf(rd.nextInt(9));
        }
        if(canZeroStart)
            return num;
        else{
            while(num.startsWith("0")){
                num = getRandomNum(length,false);
            }
            return num;
        }
    }

    public static void main(String[] args){
        System.out.println(getRandomNum(10,false));
    }


    /**
     * 获取length长度的随机数字字母混合验证码
     * @param length
     * @return
     */
    public static String getRandomString(int length) { //length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString().toUpperCase();
    }
}
